<template>
  <div class="aimodel-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="aimodel-dialog"
      draggable
    >
      <el-form ref="aimodelRef" :model="state.form" :rules="state.rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="ainame">
              <el-input v-model="state.form.ainame" placeholder="产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品类型" prop="aitype">
              <el-input v-model="state.form.aitype" placeholder="产品类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型名称" prop="aimodelname">
              <el-select v-model="state.form.aimodelname" placeholder="选择模型" style="width: 100%">
                <el-option v-for="item in dictsData[0]" :key="`aimodelname_${item.substance}`" :label="item.title" :value="item.substance" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="场景设定" prop="aicjsd">
              <el-input type="textarea" :rows="15" v-model="state.form.aicjsd" placeholder="场景设定" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="回复多样性" prop="aitemperature">
              <el-input v-model="state.form.aitemperature" placeholder="回复多样性" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生成图片大小" prop="aiimgsize">
              <el-input v-model="state.form.aiimgsize" placeholder="生成图片大小" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单次生成数量" prop="aiimgnum">
              <el-input v-model="state.form.aiimgnum" placeholder="单次生成数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大会话长度" prop="aimaxtokens">
              <el-input v-model="state.form.aimaxtokens" placeholder="最大会话长度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="启用上下文" prop="aiiscontext">
              <el-switch v-model="state.form.aiiscontext" inline-prompt :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否启用" prop="aistatus">
              <el-switch v-model="state.form.aistatus" inline-prompt :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="组织编号" prop="aiorganization">
              <el-input v-model="state.form.aiorganization" placeholder="组织编号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分配apikey" prop="aikey">
              <el-input v-model="state.form.aikey" placeholder="分配apikey" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useAImodelManageStore from '@store/modules/aimodelManage'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

const aimodelManageStore = useAImodelManageStore()
const {proxy} = getCurrentInstance()
const aimodelRef = ref(null)

const dictsData = ref(proxy.useDict('OpenAI-Version'))

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    ainame: '',
    aitype: '',
    aimodelname: '',
    aicjsd: '',
    aitemperature: '',
    aiimgsize: '',
    aiimgnum: '',
    aiorganization: '',
    aiiscontext: '',
    aimaxtokens: '',
    aistatus: '',
    aikey: ''
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  }
})

const emits = defineEmits(['refresh'])

const getDataView = (aiid) => {
  nextTick(async () => {
    const result = await aimodelManageStore.view({aiid: aiid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  aimodelRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.aiid = state.editView.aiid
        await aimodelManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await aimodelManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodelRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  aimodelRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.aiid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.ainame}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
